import React, { useEffect, useState } from "react";
import {updateUser} from '../../services/user'
import {useSelector,useDispatch} from 'react-redux';
import { setUpdateVisible } from "../../store/slices/userSlice";
import { Modal, Button, Form, Radio , Input,message, Upload ,Select} from "antd";
export default function Update({show}) {
  let updateVisible = useSelector(state => state.user.updateVisible);
  let userObj = useSelector(state => state.user.userObj);
  const children = [];
  let dispatch = useDispatch();
  const handleChange = (value) => {
    console.log(`selected ${value}`);
  };
  const [form] = Form.useForm();
 
    console.log(userObj);

  const handleOk = async() => {
    
    let values = form.getFieldsValue();
    console.log(values);
    if (values.pwd=="") {
      console.log("密码为空",userObj.pwd);
      // await updateUser({...values,id:userObj._id,pwd:userObj.pwd});
    }else{
      await updateUser({...values,id:userObj._id});
    }
    
    show();
    dispatch(setUpdateVisible(false));
  };

  const handleCancel = () => {
    dispatch(setUpdateVisible(false));
  };
  form.setFieldsValue({...userObj,pwd:""})
  return (
    <>
    
    <Modal
      title="修改学生"
      visible={updateVisible}
      onOk={handleOk}
      onCancel={handleCancel}
    >
      <Form
          form={form}
          name="add"
          labelCol={{
            span: 8,
          }}
          wrapperCol={{
            span: 16,
          }}
          initialValues={{
            remember: true,
          }}
          autoComplete="off"
        >
          <Form.Item
            label="用户名"
            name="username"
            hasFeedback
            rules={[
              {
                required: true,
                message: "请输入用户名!",
              },
            ]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="密码"
            name="pwd"
            rules={[
              {
                required: true,
                message: "请输入密码!",
              },
            ]}
            hasFeedback
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="昵称"
            name="nickname"
            rules={[
              {
                required: true,
                message: "请输入昵称!",
              },
            ]}
            hasFeedback
          >
            <Input />
          </Form.Item>
          <Form.Item
            label="管理员状态"
            name="privilege"
            rules={[
              {
                required: true,
                message: "请输入状态!(是/否)",
              },
            ]}
            hasFeedback
          >
            <Input />
          </Form.Item>
          <Form.Item label="地址" name="address">
            <Select
              mode="tags"
              style={{
                width: "100%",
              }}
              placeholder="Tags Mode"
              onChange={handleChange}
            >
              {children}
            </Select>
          </Form.Item>
        </Form>
    </Modal>
  </>
  );
}
